//
//  EncodingSelectViewController.m
//  textReader
//
//  Created by kyocooro on 2/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EncodingSelectViewController.h"

// Kludge - Can't get localizedNameOfStringEncoding to work properly ...
TREncoding trEncodings[] = {
	{(kCFStringEncodingNonLossyASCII), @"NonLossyASCII"},
	{(kCFStringEncodingISOLatin1), @"ISOLatin1"},
	{(kCFStringEncodingISOLatin2), @"ISOLatin2"},
	{(kCFStringEncodingISOLatin3), @"ISOLatin3"},
	{(kCFStringEncodingISOLatin4), @"ISOLatin4"},
	{(kCFStringEncodingISOLatinCyrillic), @"ISOLatinCyrillic"},
	{(kCFStringEncodingISOLatinArabic), @"ISOLatinArabic"},
	{(kCFStringEncodingISOLatinGreek), @"ISOLatinGreek"},
	{(kCFStringEncodingISOLatinHebrew), @"ISOLatinHebrew"},
	{(kCFStringEncodingISOLatin5), @"ISOLatin5"},
	{(kCFStringEncodingISOLatin6), @"ISOLatin6"},
	{(kCFStringEncodingISOLatinThai), @"ISOLatinThai"},
	{(kCFStringEncodingISOLatin7), @"ISOLatin7"},
	{(kCFStringEncodingISOLatin8), @"ISOLatin8"},
	{(kCFStringEncodingISOLatin9), @"ISOLatin9"},
	{(kCFStringEncodingISOLatin10), @"ISOLatin10"},
	{(kCFStringEncodingMacRoman), @"MacRoman"},
	{(kCFStringEncodingUnicode), @"Unicode"},
	{(kCFStringEncodingUTF8), @"UTF8"},
	{(kCFStringEncodingUTF16), @"UTF16"},
	{(kCFStringEncodingUTF16BE), @"UTF16BE"},
	{(kCFStringEncodingUTF16LE), @"UTF16LE"},
	{(kCFStringEncodingUTF32), @"UTF32"},
	{(kCFStringEncodingUTF32BE), @"UTF32BE"},
	{(kCFStringEncodingUTF32LE), @"UTF32LE"},
	{(kCFStringEncodingWindowsLatin1), @"WindowsLatin1"},
	{(kCFStringEncodingWindowsLatin2), @"WindowsLatin2"},
	{(kCFStringEncodingWindowsCyrillic), @"WindowsCyrillic"},
	{(kCFStringEncodingWindowsGreek), @"WindowsGreek"},
	{(kCFStringEncodingWindowsLatin5), @"WindowsLatin5"},
	{(kCFStringEncodingWindowsHebrew), @"WindowsHebrew"},
	{(kCFStringEncodingWindowsArabic), @"WindowsArabic"},
	{(kCFStringEncodingWindowsBalticRim), @"WindowsBalticRim"},
	{(kCFStringEncodingWindowsVietnamese), @"WindowsVietnamese"},
	{(kCFStringEncodingWindowsKoreanJohab), @"WindowsKoreanJohab"},
	{(kCFStringEncodingGB_2312_80), @"GB_2312_80"},
	{(kCFStringEncodingGBK_95), @"GBK_95"},
	{(kCFStringEncodingGB_18030_2000), @"GB_18030_2000"},
	{(kCFStringEncodingMacJapanese), @"MacJapanese"},
	{(kCFStringEncodingMacChineseTrad), @"MacChineseTrad"},
	{(kCFStringEncodingMacKorean), @"MacKorean"},
	{(kCFStringEncodingMacArabic), @"MacArabic"},
	{(kCFStringEncodingMacHebrew), @"MacHebrew"},
	{(kCFStringEncodingMacGreek), @"MacGreek"},
	{(kCFStringEncodingMacCyrillic), @"MacCyrillic"},
	{(kCFStringEncodingMacDevanagari), @"MacDevanagari"},
	{(kCFStringEncodingMacGurmukhi), @"MacGurmukhi"},
	{(kCFStringEncodingMacGujarati), @"MacGujarati"},
	{(kCFStringEncodingMacOriya), @"MacOriya"},
	{(kCFStringEncodingMacBengali), @"MacBengali"},
	{(kCFStringEncodingMacTamil), @"MacTamil"},
	{(kCFStringEncodingMacTelugu), @"MacTelugu"},
	{(kCFStringEncodingMacKannada), @"MacKannada"},
	{(kCFStringEncodingMacMalayalam), @"MacMalayalam"},
	{(kCFStringEncodingMacSinhalese), @"MacSinhalese"},
	{(kCFStringEncodingMacBurmese), @"MacBurmese"},
	{(kCFStringEncodingMacKhmer), @"MacKhmer"},
	{(kCFStringEncodingMacThai), @"MacThai"},
	{(kCFStringEncodingMacLaotian), @"MacLaotian"},
	{(kCFStringEncodingMacGeorgian), @"MacGeorgian"},
	{(kCFStringEncodingMacArmenian), @"MacArmenian"},
	{(kCFStringEncodingMacChineseSimp), @"MacChineseSimp"},
	{(kCFStringEncodingMacTibetan), @"MacTibetan"},
	{(kCFStringEncodingMacMongolian), @"MacMongolian"},
	{(kCFStringEncodingMacEthiopic), @"MacEthiopic"},
	{(kCFStringEncodingMacCentralEurRoman), @"MacCentralEurRoman"},
	{(kCFStringEncodingMacVietnamese), @"MacVietnamese"},
	{(kCFStringEncodingMacExtArabic), @"MacExtArabic"},
	{(kCFStringEncodingMacSymbol), @"MacSymbol"},
	{(kCFStringEncodingMacDingbats), @"MacDingbats"},
	{(kCFStringEncodingMacTurkish), @"MacTurkish"},
	{(kCFStringEncodingMacCroatian), @"MacCroatian"},
	{(kCFStringEncodingMacIcelandic), @"MacIcelandic"},
	{(kCFStringEncodingMacRomanian), @"MacRomanian"},
	{(kCFStringEncodingMacCeltic), @"MacCeltic"},
	{(kCFStringEncodingMacGaelic), @"MacGaelic"},
	{(kCFStringEncodingMacFarsi), @"MacFarsi"},
	{(kCFStringEncodingMacUkrainian), @"MacUkrainian"},
	{(kCFStringEncodingMacInuit), @"MacInuit"},
	{(kCFStringEncodingMacVT100), @"MacVT100"},
	{(kCFStringEncodingMacHFS), @"MacHFS"},
	{(kCFStringEncodingDOSLatinUS), @"DOSLatinUS"},
	{(kCFStringEncodingDOSGreek), @"DOSGreek"},
	{(kCFStringEncodingDOSBalticRim), @"DOSBalticRim"},
	{(kCFStringEncodingDOSLatin1), @"DOSLatin1"},
	{(kCFStringEncodingDOSGreek1), @"DOSGreek1"},
	{(kCFStringEncodingDOSLatin2), @"DOSLatin2"},
	{(kCFStringEncodingDOSCyrillic), @"DOSCyrillic"},
	{(kCFStringEncodingDOSTurkish), @"DOSTurkish"},
	{(kCFStringEncodingDOSPortuguese), @"DOSPortuguese"},
	{(kCFStringEncodingDOSIcelandic), @"DOSIcelandic"},
	{(kCFStringEncodingDOSHebrew), @"DOSHebrew"},
	{(kCFStringEncodingDOSCanadianFrench), @"DOSCanadianFrench"},
	{(kCFStringEncodingDOSArabic), @"DOSArabic"},
	{(kCFStringEncodingDOSNordic), @"DOSNordic"},
	{(kCFStringEncodingDOSRussian), @"DOSRussian"},
	{(kCFStringEncodingDOSGreek2), @"DOSGreek2"},
	{(kCFStringEncodingDOSThai), @"DOSThai"},
	{(kCFStringEncodingDOSJapanese), @"DOSJapanese"},
	{(kCFStringEncodingDOSChineseSimplif), @"DOSChineseSimplif"},
	{(kCFStringEncodingDOSKorean), @"DOSKorean"},
	{(kCFStringEncodingDOSChineseTrad), @"DOSChineseTrad"},
	{(kCFStringEncodingASCII), @"ASCII"},
	{(kCFStringEncodingANSEL), @"ANSEL"},
	{(kCFStringEncodingJIS_X0201_76), @"JIS_X0201_76"},
	{(kCFStringEncodingJIS_X0208_83), @"JIS_X0208_83"},
	{(kCFStringEncodingJIS_X0208_90), @"JIS_X0208_90"},
	{(kCFStringEncodingJIS_X0212_90), @"JIS_X0212_90"},
	{(kCFStringEncodingJIS_C6226_78), @"JIS_C6226_78"},
	{(kCFStringEncodingShiftJIS_X0213_00), @"ShiftJIS_X0213_00"},
	{(kCFStringEncodingShiftJIS_X0213_MenKuTen), @"ShiftJIS_X0213_MenKuTen"},
	{(kCFStringEncodingKSC_5601_87), @"KSC_5601_87"},
	{(kCFStringEncodingKSC_5601_92_Johab), @"KSC_5601_92_Johab"},
	{(kCFStringEncodingCNS_11643_92_P1), @"CNS_11643_92_P1"},
	{(kCFStringEncodingCNS_11643_92_P2), @"CNS_11643_92_P2"},
	{(kCFStringEncodingCNS_11643_92_P3), @"CNS_11643_92_P3"},
	{(kCFStringEncodingISO_2022_JP), @"ISO_2022_JP"},
	{(kCFStringEncodingISO_2022_JP_2), @"ISO_2022_JP_2"},
	{(kCFStringEncodingISO_2022_JP_1), @"ISO_2022_JP_1"},
	{(kCFStringEncodingISO_2022_JP_3), @"ISO_2022_JP_3"},
	{(kCFStringEncodingISO_2022_CN), @"ISO_2022_CN"},
	{(kCFStringEncodingISO_2022_CN_EXT), @"ISO_2022_CN_EXT"},
	{(kCFStringEncodingISO_2022_KR), @"ISO_2022_KR"},
	{(kCFStringEncodingEUC_JP), @"EUC_JP"},
	{(kCFStringEncodingEUC_CN), @"EUC_CN"},
	{(kCFStringEncodingEUC_TW), @"EUC_TW"},
	{(kCFStringEncodingEUC_KR), @"EUC_KR"},
	{(kCFStringEncodingShiftJIS), @"ShiftJIS"},
	{(kCFStringEncodingKOI8_R), @"KOI8_R"},
	{(kCFStringEncodingBig5), @"Big5"},
	{(kCFStringEncodingMacRomanLatin1), @"MacRomanLatin1"},
	{(kCFStringEncodingHZ_GB_2312), @"HZ_GB_2312"},
	{(kCFStringEncodingBig5_HKSCS_1999), @"Big5_HKSCS_1999"},
	{(kCFStringEncodingVISCII), @"VISCII"},
	{(kCFStringEncodingKOI8_U), @"KOI8_U"},
	{(kCFStringEncodingBig5_E), @"Big5_E"},
	{(kCFStringEncodingNextStepLatin), @"NextStepLatin"},
	{(kCFStringEncodingNextStepJapanese), @"NextStepJapanese"},
	{(kCFStringEncodingEBCDIC_US), @"EBCDIC_US"},
	{(kCFStringEncodingEBCDIC_CP037), @"EBCDIC_CP037"}
};
int trEncodingsL = (sizeof(trEncodings)/sizeof(*trEncodings));


@implementation EncodingSelectViewController
@synthesize delegate = _delegate;
// Return the string associated with a given encoding
- (NSString *)stringFromEncoding:(CFStringEncoding)enc 
{
	
    int i;
    // Try to find the encoding
    for (i = 0; i < trEncodingsL; i++)
    {
        if (trEncodings[i].encoding == enc)
            return trEncodings[i].name;
    }
	
    return TEXTREADER_ENC_NONE_NAME;
	
} // stringFromEncoding


// Return the numeric encoding associated with the given string
- (CFStringEncoding)encodingFromString:(NSString *)string {
    int i;
	
    // Try to find the encoding
    for (i = 0; i < trEncodingsL; i++)
    {
        if ([string compare:trEncodings[i].name] == NSOrderedSame)
            return trEncodings[i].encoding;
    }
	
    return TEXTREADER_ENC_NONE;
	
} // encodingFromString
#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return trEncodingsL;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"EncodingCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = trEncodings[indexPath.row].name;
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[_delegate didSelectEncoding:trEncodings[indexPath.row].encoding];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

